package edu.wust.homework.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.wust.homework.entity.Student;
import edu.wust.homework.mapper.StudentMapper;
import edu.wust.homework.service.IStudentService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService extends ServiceImpl<StudentMapper, Student>implements IStudentService {

    @Override
    public Student getStuByUserName(String userName){
        return this.baseMapper.getStuByUserName(userName);
    }

    @Override
    public int register(Student s){
        //先查看一下账号，是否重复
        Student u=this.getStuByUserName(s.getUserName());
        if (u!=null){
            return -1;
        }
        System.out.println("出刷ask了-："+u);
        return this.baseMapper.insertStudent(s);
    }

    @Override
    public int modifyPassword(String userName,String oldPassword,String newPassword){
        return this.baseMapper.modifyPassword(userName,oldPassword,newPassword);
    }

    @Override
    public int deleteById(Integer id){
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int deleteByUsername(String userName){
        return this.baseMapper.deleteByUsername(userName);
    }

    @Override
    public List<Student> getAllStudents(){
        return this.baseMapper.getAllStudents();
    }

    @Override
    public List<Student> getAllStudentsByPage(long current, long size){
        Page<Student> page = new Page<>(current, size);
        return this.baseMapper.getAllStudentsByPage(page);
    }


    @Override
    public boolean updateStudentInfo(Student student) {
        int rowsAffected = this.baseMapper.updateStudentInfo(student);
        return rowsAffected > 0;
    }

}
